package com.atguigu.gmall.order.Listener.mq;

import com.atguigu.gmall.common.constants.RabbitMqConst;
import com.atguigu.gmall.common.util.JsonUtil;
import com.atguigu.gmall.model.order.OrderDetail;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.to.ware.WareDetailsTo;
import com.atguigu.gmall.model.to.ware.WareOrderTo;
import com.atguigu.gmall.order.service.OrderInfoService;
import com.atguigu.gmall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2024/10/18 1:15
 */
@Slf4j
@Service
public class OrderPaidListener {
    @Autowired
    OrderService orderService;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    OrderInfoService orderInfoService;
    //监听支付成功,更改已支付状态,并且向库存系统发送减库存消息
    @RabbitListener(queues = {RabbitMqConst.ORDER_PAID_QUEUE})
    public void orderUpdateToPaid(Message message, Channel channel) {

        String outTradeNo = null;
        try {
            outTradeNo = new String(message.getBody());
            //更改状态为paid已支付
            orderService.checkOrderStatus(outTradeNo);
            //向库存系统发消息,减库存
            WareOrderTo wareOrderTo =prepareWareOrderTo(outTradeNo);
            String json = JsonUtil.obj2Json(wareOrderTo);
            rabbitTemplate.convertAndSend(RabbitMqConst.WARE_EVENT_EXCHANGE,RabbitMqConst.WARE_RK_STOCK,json);
            //回复消费成功
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            log.error("mq发生异常,更改订单为paid状态可能失败:{},发生的异常:{}", outTradeNo, e);
        }

    }

    private WareOrderTo prepareWareOrderTo(String outTradeNo) {
        WareOrderTo wareOrderTo = new WareOrderTo();
        OrderInfo orderInfo =orderInfoService.getOrderInfoAndDetails(outTradeNo);
        wareOrderTo.setOrderId(orderInfo.getId());
        wareOrderTo.setConsignee(orderInfo.getConsignee());
        wareOrderTo.setConsigneeTel(orderInfo.getConsigneeTel());
        wareOrderTo.setOrderComment(orderInfo.getOrderComment());
        wareOrderTo.setOrderBody(orderInfo.getTradeBody());
        wareOrderTo.setDeliveryAddress(orderInfo.getDeliveryAddress());
        wareOrderTo.setPaymentWay("2");
        List<OrderDetail> orderDetailList = orderInfo.getOrderDetailList();
        List<WareDetailsTo> wareDetailsToList = orderDetailList.stream().map((orderDetail) -> {
            WareDetailsTo wareDetailsTo = new WareDetailsTo();
            wareDetailsTo.setSkuId(orderDetail.getSkuId());
            wareDetailsTo.setSkuNum(orderDetail.getSkuNum());
            wareDetailsTo.setSkuName(orderDetail.getSkuName());
            return wareDetailsTo;
        }).collect(Collectors.toList());
        wareOrderTo.setDetails(wareDetailsToList);

        return wareOrderTo;
    }
}
